
package cartagows.wsframework.wssecurity.policy.model;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.neethi.PolicyComponent;

import cartagows.wsframework.wssecurity.policy.SP11Constants;
import cartagows.wsframework.wssecurity.policy.SP12Constants;
import cartagows.wsframework.wssecurity.policy.SPConstants;


public class HttpsToken extends Token {

    public HttpsToken(int version){
        setVersion(version);
    }
    
    private boolean requireClientCertificate = false;

    public boolean isRequireClientCertificate() {
        return requireClientCertificate;
    }

    public void setRequireClientCertificate(boolean requireClientCertificate) {
        this.requireClientCertificate = requireClientCertificate;
    }

    public QName getName() {
        if (version == SPConstants.SP_V12) {
            return SP12Constants.HTTPS_TOKEN;
        } else {
            return SP11Constants.HTTPS_TOKEN;
        }
    }

    public PolicyComponent normalize() {
        throw new UnsupportedOperationException();
    }

    public void serialize(XMLStreamWriter writer) throws XMLStreamException {

        String localname = getName().getLocalPart();
        String namespaceURI = getName().getNamespaceURI();

        String prefix = writer.getPrefix(namespaceURI);
        if (prefix == null) {
            prefix = getName().getPrefix();
            writer.setPrefix(prefix, namespaceURI);
        }

        // <sp:HttpsToken
        writer.writeStartElement(prefix, localname, namespaceURI);

        // RequireClientCertificate=".."
        writer
                .writeAttribute(SPConstants.REQUIRE_CLIENT_CERTIFICATE.getLocalPart(), Boolean
                        .toString(isRequireClientCertificate()));

        writer.writeEndElement();
        // </sp:HttpsToken>
    }
}
